package com.gzy.literature.controller;

import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.UserViews;
import com.gzy.literature.service.UserViewsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;

@RestController
@RequestMapping("userViews")
public class UserViewsController extends HttpServlet {
    /**
     * 服务对象
     */
    @Resource
    private UserViewsService userViewsService;

    /**
     * 分页查询
     *
     * @param userViews 筛选条件
     * @param pageNum    页码，从1开始
     * @param pageSize   页面大小
     * @return 查询结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageInfo<UserViews>> queryByPage(@RequestBody(required = false) UserViews userViews,
                                                           @RequestParam(defaultValue = "1") int pageNum,
                                                           @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        if (pageSize <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        PageInfo<UserViews> pageInfo = this.userViewsService.queryEll(userViews, pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/{userId}")
    public ResponseEntity<UserViews> queryById(@PathVariable("userId") Integer userId) {
        // 手动验证userId（可选）
        if (userId == null || userId <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        return ResponseEntity.ok(this.userViewsService.queryById(userId));
    }

    /**
     * 新增数据
     *
     * @param userViews 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserViews> add(@RequestBody UserViews userViews) {
        // 手动验证userViews（示例）
        if (userViews == null || userViews.getUserId() == null || userViews.getViewTime() == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        return ResponseEntity.ok(this.userViewsService.insert(userViews));
    }

    /**
     * 编辑数据
     *
     * @param userViews 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserViews> edit(@RequestBody UserViews userViews) {
        if (userViews == null || userViews.getUserId() == null || userViews.getViewTime() == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        // 可以根据业务需要添加更多的字段验证
        // 例如：if (userViews.getSomeOtherField() == null) { ... }

        return ResponseEntity.ok(this.userViewsService.update(userViews));
    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{userId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("userId") Integer userId) {
        // 手动验证userId（可选）
        if (userId == null || userId <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }

        return ResponseEntity.ok(this.userViewsService.deleteById(userId));
    }
}